package com.ziq.zcamp.dialog

import android.app.Activity
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import com.blankj.utilcode.util.ImageUtils
import com.ziq.base.base.BaseDialog
import com.ziq.zcamp.databinding.DialogPostTypeSelectorBinding
import com.ziq.base.extension.*

class PostTypeSelectorDialog(val activity: Activity) : BaseDialog(activity) {


    private val binding: DialogPostTypeSelectorBinding by binding()
    var callBack: ActionCallBack? = null

    override fun initHeight(): Int {
        return ViewGroup.LayoutParams.MATCH_PARENT
    }

    override fun initWidth(): Int {
        return ViewGroup.LayoutParams.MATCH_PARENT
    }

    override fun initGravity(): Int {
        return Gravity.BOTTOM or Gravity.CENTER
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        window?.decorView?.let {
            window?.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        }
        val uiOptions = (View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_IMMERSIVE
                or View.SYSTEM_UI_FLAG_FULLSCREEN)
        window?.getDecorView()?.setSystemUiVisibility(uiOptions)

        binding.root
        binding.root.click {
            dismiss()
        }

        try {
            val view = activity.window.decorView
            view.isDrawingCacheEnabled = true
            view.buildDrawingCache()
            val b = view.drawingCache
            binding.bgBlur.background = BitmapDrawable(null, ImageUtils.fastBlur(b, 0.2F, 1F))
            view.isDrawingCacheEnabled = false
            view.destroyDrawingCache()
        }catch (e: Exception){

        }

        binding.llActionDynamic.click {
            callBack?.onActionClick(this, 0)
        }
        binding.llActionVideo.click {
            callBack?.onActionClick(this, 1)
        }
        binding.llActionArticle.click {
            callBack?.onActionClick(this, 2)
        }

        setAnimateView(binding.llAction)
    }


    class Builder(val context: Activity) {

        var callBack: ActionCallBack? = null

        fun setCallback(cb: ActionCallBack) : Builder{
            callBack = cb
            return this
        }


        fun build(): PostTypeSelectorDialog {
            val dialog = PostTypeSelectorDialog(context)
            dialog.callBack = callBack
            return dialog
        }

    }

    interface ActionCallBack{
        fun onActionClick(dialog: PostTypeSelectorDialog, position: Int)
    }


}